package com.example.job.Activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.job.R;

import com.example.job.db.userdb;

public class RegisterActivity extends AppCompatActivity {
    private EditText re_username;
    private EditText re_password;
    private EditText check_psd;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register);

        re_username = findViewById(R.id.re_user);
        re_password = findViewById(R.id.re_password);
        check_psd = findViewById(R.id.check_psd);


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        findViewById(R.id.back_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = re_username.getText().toString();
                String password = re_password.getText().toString();
                String check = check_psd.getText().toString();

                if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
                    Toast.makeText(RegisterActivity.this,"请输入用户名和密码",Toast.LENGTH_SHORT).show();
                } else if (!password.equals(check)) {
                    Toast.makeText(RegisterActivity.this,"两次密码不一致",Toast.LENGTH_SHORT).show();
                } else{
                    int row = (int) userdb.getInstance(RegisterActivity.this).register(username,password);
                    if(row>0){
                        Toast.makeText(RegisterActivity.this,"注册成功，请重新登录",Toast.LENGTH_SHORT).show();
                        finish();
                    }


                }
            }
        });
    }
}